Charles Lanfear
May 24, 2017
ggmap for mashing up maps with ggplot2ggrepelggplot2If you are interested in mapping, GIS, and geospatial analysis in R, acquire this book.
You may also consider taking Jon Wakefield's CSSS 554: Statistical Methods for Spatial Data, however it is challenging and focuses more heavily on statistics than mapping.
ggmap is a package that goes with ggplot2 so that you can plot spatial data directly onto map images downloaded from Google Maps, OpenStreetMap, and Stamen Maps.
What this package does for you:
get_map()) at the location and scale you wantggplot objectggplot layers like points, 2D density plots, text annotationsIn Week 5, we looked at types of incidents the Seattle Police Department responded to in a single day. Now, we'll look at where those were.
library(tidyverse); library(ggmap)
spd_raw <- read_csv("https://clanfear.github.io/CSSS508/Seattle_Police_Department_911_Incident_Response.csv")
qmplot will automatically set the map region based on your data:
qmplot(data = spd_raw, x = Longitude, y = Latitude, color = I("firebrick"), alpha = I(0.5))
qmap(location = "mary gates hall university of washington", zoom = 15, maptype = "watercolor", source = "stamen")
Both qmplot() and qmap() are wrappers for a function called get_map() that retrieves a base map layer. Some options:
location = search query or numeric vector of longitude and latitudezoom = a zoom level (3 = continent, 10 = city, 21 = building)source = "google", "osm", "stamen"maptype =
"terrain", "terrain-background", "satellite", "roadmap", "hybrid""watercolor", "toner", "toner-background", "toner-lite"color = "color" or "bw"qmap(location = "pike place market", zoom = 14, maptype = "toner-background", source = "stamen")
qmap(location = "seattle", zoom = 8, maptype = "terrain", source = "google")
Let's look at locations of incidents near downtown.
# query the map server
downtown_map <- get_map(location = "pike place market", zoom = 14)
# grab the bounding box coordinate data frame
downtown_bb <- attributes(downtown_map)[["bb"]]
# subset the data based on bounding box
downtown_seattle_incidents <- spd_raw %>%
filter(downtown_bb[["ll.lat"]] <= Latitude &
Latitude <= downtown_bb[["ur.lat"]] &
downtown_bb[["ll.lon"]] <= Longitude &
Longitude <= downtown_bb[["ur.lon"]])
Call qmplot() with no geom(), and then add density layers:
qmplot(data = downtown_seattle_incidents, geom = "blank", x = Longitude, y = Latitude, maptype = "toner-lite", darken = 0.5) + stat_density_2d(aes(fill = ..level..), geom = "polygon", alpha = .2, color = NA) + scale_fill_gradient2("Incident concentration", low = "white", mid = "yellow", high = "red")
Let's label the assaults and robberies specifically in downtown:
assaults <- downtown_seattle_incidents %>% mutate(assault_label = ifelse(`Event Clearance Group` %in% c("ASSAULTS", "ROBBERY"), `Event Clearance Description`, "")) %>% filter(assault_label != "")
Now let's plot the events and label these specifically using geom_label() (geom_text() also works without the background/border):
qmplot(data = downtown_seattle_incidents, x = Longitude, y = Latitude, maptype = "toner-lite", color = I("firebrick"), alpha = I(0.5)) + geom_label(data = assaults, aes(label = assault_label))
You can also try geom_label_repel() or geom_text_repel() if you install and load in the ggrepel() package to fix overlaps:
library(ggrepel)
qmplot(data = downtown_seattle_incidents, x = Longitude, y = Latitude, maptype = "toner-lite", color = I("firebrick"), alpha = I(0.5)) + geom_label_repel(data = assaults, aes(label = assault_label), fill = "black", color = "white", segment.color = "black")
It is also common (and easy) to plot geospatial data using standard ggplot2 functions. Just provide x and y
coordinates to use to draw points or lines.
You need only provide ggplot the following:
x aestheticy aestheticsea_tract_data <- read_csv("https://raw.githubusercontent.com/clanfear/CSSS508/master/Lectures/Week9/sea_tract_data.csv")
ggplot(sea_tract_data, aes(x=long, y=lat, group = group, fill=con_disdvntg)) +
geom_polygon() + scale_fill_gradient(low="white", high="darkred") +
coord_equal() + geom_path(color = "black", linetype=1) +
theme(axis.title=element_blank(), axis.text = element_blank(), panel.grid.major =
element_blank(), panel.grid.minor = element_blank(), panel.background =
element_blank(), axis.ticks = element_blank(), axis.line = element_blank()) +
labs(title="Concentrated Disadvantage", fill="Disadvantage\n")
Use the Lab/HW 7 template to practice making maps of the restaurant inspection data. Save your work when you're done by emailing it to yourself. If you wish to submit it for bonus points, turn it in via Canvas by midnight on Tuesday the 30th.